﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CourierManager
{
    public class TransportUnit : ICapacitable
    {
        private string pathToDatabase = "../../../CourierManager/TransportUnits.txt";

        public string PathToDatabase
        {
            get { return pathToDatabase; }
            set { pathToDatabase = value; }
        }

        public string Type { get; set; }

        public string Brand { get; set; }

        public string LicensePlate { get; set; }

        public decimal Load { get; set; }

        public Driver CurrentDriver { get; set; }

        public int Capacity { get; set; }

        public TransportUnitState State { get; set; }

        public List<string> AllUnits { get; set; }

        public List<Freight> LoadedFreights { get; set; }

        public Route CurrentRoute { get; set; }

        public bool IsANewTransportUnit { get; set; }

        public double FuelConsumption { get; set; }

        public string ProductionDate { get; set; }

        public TransportUnit(string licensePlate, int capacity, TransportUnitState state = TransportUnitState.Available)
        {
            this.LicensePlate = licensePlate;
            this.Capacity = capacity;
            this.State = state;
            this.IsANewTransportUnit = true;
            this.LoadedFreights = new List<Freight>();
        }

        public TransportUnit(string brand, string licensePlate, int capacity)
        {
            this.Brand = brand;
            this.LicensePlate = licensePlate;
            this.Capacity = capacity;
        }

        public TransportUnit()
            : this(null, 0)
        {
        }

        public void WriteInDatabase()
        {
            if (this.LicensePlate != null)
            {
                if (!String.IsNullOrEmpty(this.LicensePlate))
                {
                    this.AllUnits = GetAllTransportUnits();
                    StreamWriter writeInFile = new StreamWriter(this.PathToDatabase);
                    using (writeInFile)
                    {
                        string vehicleType = "Truck";
                        if (this is Van)
                        {
                            vehicleType = this.GetType().Name;
                        }
                        if (this.AllUnits.Count != 0)
                        {
                            foreach (string currentRow in this.AllUnits)
                            {
                                if (currentRow.Split('*')[1] != this.LicensePlate)
                                {
                                    if (!String.IsNullOrEmpty(currentRow))
                                    {
                                        writeInFile.WriteLine(currentRow);
                                    }
                                }
                                else
                                {
                                    writeInFile.Write("{0}*{1}*{2}*{3}",
                                        this.Type, this.LicensePlate, this.Load, this.State);

                                    if (this.State == TransportUnitState.OnACourse)
                                    {
                                        writeInFile.WriteLine("*{0}*{1}", this.CurrentRoute.Name, this.CurrentDriver.IDNumber);
                                    }
                                    else
                                    {
                                        writeInFile.WriteLine();
                                    }
                                    this.IsANewTransportUnit = false;
                                }
                            }
                        }

                        if (IsANewTransportUnit)
                        {
                            writeInFile.Write("{0}*{1}*{2}*{3}",
                                           vehicleType, this.LicensePlate, this.Load, this.State);
                        }
                    }
                }
            }
        }

        public virtual void SaveVehicleData()
        {
        }

        public List<string> GetAllTransportUnits()
        {
            List<string> allUnits = new List<string>();
            if (File.Exists(this.PathToDatabase))
            {
                StreamReader readFromFile = new StreamReader(this.PathToDatabase);

                using (readFromFile)
                {
                    string currentRow = readFromFile.ReadLine();
                    while (currentRow != null)
                    {
                        allUnits.Add(currentRow);
                        currentRow = readFromFile.ReadLine();
                    }
                }
            }
            return allUnits;
        }

        public void DeleteTransportUnit()
        {
            this.AllUnits = GetAllTransportUnits();
            StreamWriter writeInFile = new StreamWriter(this.PathToDatabase);
            foreach (string vehicleData in this.AllUnits)
            {
                if (vehicleData.Split('*')[0] != this.LicensePlate)
                {
                    writeInFile.WriteLine(vehicleData);
                }
            }
        }

        public bool AddLoad(decimal load)
        {
            //Check if the transport unit can carry more load.this.Capacity - 50 because the unit cannot be filled at 100%
            if (this.Load + load < this.Capacity - 50)
            {
                this.Load += load;
                return true;
            }
            return false;
        }
    }
}
